package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    public Result add( @RequestBody CheckItem checkItem){

        try {
            checkItemService.add(checkItem);
//            System.out.println(checkItem);
        } catch (Exception e) {
            e.printStackTrace();

            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

//        Integer currentPage = queryPageBean.getCurrentPage();
//        Integer pageSize = queryPageBean.getPageSize();
//        String queryString = queryPageBean.getQueryString();

        PageResult result = null;
        try {
            result = checkItemService.pageQuery(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
           // return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        //System.out.println();
        //return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,result );
        return result;
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){

        try {
            int result = checkItemService.delete(id);
//            System.out.println("id"+id  +", result" +result);
            if (result==-1){
                return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL );
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL );
        }

        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS );
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){

        try {
            checkItemService.update(checkItem);
            System.out.println(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL );
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){

        try {
            List<CheckItem> checkItems = checkItemService.findAll();

            if (checkItems.size()>0&& checkItems != null){
                return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS ,checkItems);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL,new ArrayList<>());
    }


}
